package com.demo.controller;

import com.demo.common.Result;
import com.demo.pojo.dto.CoursePlanListDTO;
import com.demo.pojo.entity.CoursePlan;
import com.demo.service.CoursePlanService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/coursePlan")
public class CoursePlanController {

    private final CoursePlanService coursePlanService;

    public CoursePlanController(CoursePlanService coursePlanService) {
        this.coursePlanService = coursePlanService;
    }

    @GetMapping("/select")
    public Result<CoursePlan> select(@RequestParam Long id) {
        return Result.success(coursePlanService.selectById(id));
    }

    @PostMapping("/save")
    public Result<Void> save(@RequestBody CoursePlan coursePlan) {
        coursePlanService.saveCoursePlan(coursePlan);
        return Result.success();
    }

    @PutMapping("/update")
    public Result<Void> update(@RequestBody CoursePlan coursePlan) {
        coursePlanService.updateCoursePlan(coursePlan);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam Long id) {
        coursePlanService.removeById(id);
        return Result.success();
    }

    @GetMapping("/listPage")
    public Result<Map<String, Object>> listPage(@RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize,
                                                @RequestParam(required = false) String cpCourseId,
                                                @RequestParam(required = false) Long cpClassId,
                                                @RequestParam(required = false) Long cpTeacherId,
                                                @RequestParam(required = false) String cpRoomId,
                                                @RequestParam(required = false) Integer cpWeekday,
                                                @RequestParam(required = false) String cpStartTime,
                                                @RequestParam(required = false) String cpEndTime) {
        CoursePlanListDTO dto = new CoursePlanListDTO();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setCpCourseId(cpCourseId);
        dto.setCpClassId(cpClassId);
        dto.setCpTeacherId(cpTeacherId);
        dto.setCpRoomId(cpRoomId);
        dto.setCpWeekday(cpWeekday);
        // 让前端传字符串时也兼容，若为空则不拼条件
        if (cpStartTime != null && !cpStartTime.isEmpty()) {
            dto.setCpStartTime(java.time.LocalDateTime.parse(cpStartTime.replace(" ", "T")));
        }
        if (cpEndTime != null && !cpEndTime.isEmpty()) {
            dto.setCpEndTime(java.time.LocalDateTime.parse(cpEndTime.replace(" ", "T")));
        }
        return Result.success(coursePlanService.listPage(dto));
    }
}


